package com.xge.component_caipu

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.utils.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.internal.ViewUtils.hideKeyboard
import com.xge.common_libary.bean.CaiPuListInfoBean
import com.xge.common_libary.bean.CaiPuListInfoSection
import com.xge.common_libary.http.ApiResult
import com.xge.component_caipu.binder.ItemCaiPuContent
import com.xge.component_caipu.vm.CaiPuVM
import me.drakeet.multitype.MultiTypeAdapter
import om.xge.component_caipu.databinding.ActivitySearchBinding

class SearchActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySearchBinding
    private val viewModel: CaiPuVM by viewModels()
    private var searchContent = ""
    private var page = 1
    private var isRefresh = false
    private lateinit var adapter: MultiTypeAdapter
    private var list = ArrayList<CaiPuListInfoSection>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySearchBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.ivBack.setOnClickListener { finish() }

        initContentData()

        binding.tvSearch.setOnClickListener {
            searchContent = binding.edSearch.text.toString()
            if (TextUtils.isEmpty(searchContent)) {
                binding.edSearch.error = "请输入搜索内容"
            } else {
                binding.refreshLayout.autoRefresh()
                hideKeyboard()
            }
        }

        binding.refreshLayout.setOnRefreshListener {
            page = 1
            viewModel.searchList(searchContent, page)
            isRefresh = true
        }
        binding.refreshLayout.setOnLoadMoreListener {
            page++
            viewModel.searchList(searchContent, page)
        }


        viewModel.getCaiPuListInfo.observe(this) { result ->
            when (result) {
                is ApiResult.Error -> ToastUtils.showShort(result.message)
                is ApiResult.Exception -> ToastUtils.showShort(result.exception)
                is ApiResult.Success -> bindingContentData(result.data)
            }
        }

        binding.edSearch.setOnEditorActionListener { v, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchContent = binding.edSearch.text.toString().trim()
                if (searchContent.isNotEmpty()) {
                    hideKeyboard()
                    binding.refreshLayout.autoRefresh()
                }
                true
            } else {
                false
            }
        }
    }

    fun hideKeyboard() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0)
    }

    private fun initContentData() {
        adapter = MultiTypeAdapter(list)
        adapter.register(CaiPuListInfoSection::class.java, object : ItemCaiPuContent() {
            override fun onItemClick(item: CaiPuListInfoSection) {
                CaiPuDetailActivity.start(this@SearchActivity, item)
            }
        })
        binding.rvContent.adapter = adapter
    }

    private fun bindingContentData(data: CaiPuListInfoBean?) {
        data?.list?.let {
            if (isRefresh) {
                list.clear()
                binding.refreshLayout.finishRefresh()
                isRefresh = false
            } else {
                binding.refreshLayout.finishLoadMore()
            }
            list.addAll(it)
            adapter.notifyDataSetChanged()
        }
    }

    companion object {
        fun start(activity: Context) {
            activity.startActivity(Intent(activity, SearchActivity::class.java))
        }
    }
}